{include file="public/header" /}
</head>
<body>
    <div id="main-container">
        {include file="public/menu" /}
        <div class="main">
            <div class="notice">{$position.url}</div>
            <div class="main_content">
                <div class="title">
                    <span>{$position.name}</span>
                </div>
				<ul class="list_inline lh36 mt20 mb20">
					<li><a class="btn mr10" id="export">立即备份</a></li>
					<li><a class="btn mr10" url="{:url('optimize')}" id="optimize" >优化表</a></li>
                    <li><a class="btn mr10" url="{:url('repair')}" id="repair" >修复表</a></li>
                    <li><a class="btn mr10" href="{:url('import')}">数据库还原</a></li>
				</ul>
				<div class="table">
					<form id="export-form" method="post" action="{:url('export')}">
					<table>
						<thead>
							<tr>
								<th width="50"><input class="i-checks checkbox check-all" type="checkbox"></th>
								<th class="sort" width="200"><span>表名</span></th>
								<th class="sort"><span>数据</span></th>
								<th class="sort" width="200"><span>数据大小</span></th>
								<th class="sort" width="150"><span>创建时间</span></th>
								<th width="150"><span>备份状态</span></th>
								<th width="200"><span>操作</span></th>
							</tr>
						</thead>
						<tbody>
							{notempty name="infolist"}
		                        {volist name="infolist" id="v"}
		                        <tr>
		                            <td><input class="ids i-checks" type="checkbox" name="ids[]" value="{$v.name}"></td>
		                            <td>{$v.name}</td>
		                            <td>{$v.rows}</td>
		                            <td>{$v.data_length|format_bytes}</td>
		                            <td>{$v.create_time}</td>
		                            <td id="info">等待备份...</td>
		                            <td>
		                                <a class="btn" href="{:url('optimize',['ids'=>$v['name']])}">优化表</a>
		                                <a class="btn" href="{:url('repair',['ids'=>$v['name']])}">修复表</a>
		                            </td>
		                        </tr>
		                        {/volist}
		                    {else /}
		                        <td colspan="7" class="text-center"> 暂未发现数据库表! </td>
		                    {/notempty}
						</tbody>
					</table>
					</form>
				</div>
			</div>
		</div>
	</div>
<script>
    var laytpl,laypage;
    layui.use(['layer', 'laypage','common', 'icheck'], function () {
        var $ = layui.jquery, layer = layui.layer, common = layui.common;
        laypage = layui.laypage;
        //加载单选框样式
        $(("[type='checkbox']")).iCheck({
            checkboxClass: 'icheckbox_square-green',
        });
        //全选的实现
        $('.check-all').on('ifChecked', function (event) {
            $('input[name="ids[]"]').iCheck('check');
        });
        $('.check-all').on('ifUnchecked', function (event) {
            $('input[name="ids[]"]').iCheck('uncheck');
        });
    });


    $(function () {
        (function ($) {
            var $form = $("#export-form"), $export = $("#export"), tables, $optimize = $("#optimize"), $repair = $("#repair");
            $optimize.add($repair).click(function () {
                $.post($(this).attr('url'), $form.serialize(), function (data) {
                    if (data.code) {
                        layer.msg(data.msg,{icon:1,time:2000,shade: 0.1,});
                    } else {
                        layer.msg(data.msg,{icon:2,time:2000,shade: 0.1,});
                    }
                });
                return false;
            });

            $export.click(function () {
                $export.parent().children().prop('disabled', true);
                $export.html("正在发送备份请求...");
                $.post(
                        $form.attr("action"),
                        $form.serialize(),
                        function (data) {
                            if (data.code) {
                                tables = data.data.tables;
                                $export.html(data.msg + "开始备份，请不要关闭本页面！");
                                backup(data.data.tab);
                                window.onbeforeunload = function () {
                                    return "正在备份数据库，请不要关闭！";
                                };
                            } else {
                                layer.msg(data.msg,{icon:2,time:2000,shade: 0.1,});
                                $export.html("立即备份");
                                setTimeout(function () {
                                    $export.parent().children().prop('disabled', false);
                                }, 1500);
                            }
                        });
                return false;
            });

            function backup(tab, status) {
                status && showmsg(tab.id, "开始备份...(0%)");
                $.get($form.attr("action"), tab, function (data) {
                    if (data.code) {
                        showmsg(tab.id, data.msg);
                        if (!$.isPlainObject(data.data.tab)) {
                            $export.parent().children().prop('disabled', false);
                            $export.html("备份完成，点击重新备份");
                            window.onbeforeunload = function () {
                                return null;
                            };
                            return;
                        }
                        backup(data.data.tab, tab.id != data.data.tab.id);
                    } else {
                        layer.msg(data.msg, 0);
                        $export.html("立即备份");
                        setTimeout(function () {
                            $export.parent().children().prop('disabled', false);
                        }, 1500);
                    }
                });
            }

            function showmsg(id, msg) {
                $form.find("input[value=" + tables[id] + "]").closest("tr").find("#info").html(msg);
            }
        })(jQuery);
    });
</script>
</body>
</html>